import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'home',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard' }
      }]
  },

  {
    path: '/demo',
    component: Layout,
    name: 'demo',
    meta: { title: '展示页', icon: 'example' },
    children: [
      {
        path: 'demo',
        name: 'Demo',
        component: () => import('@/views/demo/demo'),
        meta: { title: '展示页', icon: 'example' }

      }]

  }

]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/system',
    component: Layout,
    name: 'system',
    meta: {
      title: '系统管理',
      icon: 'list'
    },
    children: [
      {
        path: 'user',
        component: () => import('@/views/system/user/index'),
        meta: { title: '用户' },
        children: [
          {
            path: 'list',
            name: 'userList',
            component: () => import('@/views/system/user/list'),
            meta: { icon: 'peoples', title: '用户管理' }
          },
          {
            path: 'edit/:id(\\d+)',
            name: 'userEdit',
            component: () => import('@/views/system/user/edit'),
            meta: { title: '详情' },
            hidden: true
          },
          {
            path: 'password/:id(\\d+)',
            name: 'userPassword',
            component: () => import('@/views/system/user/password'),
            meta: { title: '用户密码' },
            hidden: true
          }
        ]
      },
      {
        path: 'role',
        name: 'role',
        component: () => import('@/views/system/role/index'),
        meta: { title: '角色管理', icon: 'list' },
        children: [
          {
            path: 'list',
            name: 'roleList',
            component: () => import('@/views/system/role/list'),
            meta: { icon: 'user', title: '角色管理' }
          },
          {
            path: 'edit/:id(\\d+)',
            name: 'roleEdit',
            component: () => import('@/views/system/role/edit'),
            meta: { title: '详情' },
            hidden: true
          }
        ]
      },
      {
        path: 'permission',
        name: 'permission',
        meta: { title: '权限管理', icon: 'list' },
        component: () => import('@/views/system/permission/index'),
        children: [
          {
            path: 'list',
            name: 'permissionList',
            component: () => import('@/views/system/permission/list'),
            meta: { icon: 'international', title: '权限管理' }
          },
          {
            path: 'edit/:id(\\d+)',
            name: 'permissionEdit',
            component: () => import('@/views/system/permission/edit'),
            meta: { title: '详情' },
            hidden: true
          }
        ]
      },
      {
        path: 'menu',
        name: 'menu',
        meta: { title: '菜单管理', icon: 'list' },
        component: () => import('@/views/system/menu/index'),
        children: [
          {
            path: 'list',
            name: 'menuList',
            component: () => import('@/views/system/menu/list'),
            meta: { icon: 'nested', title: '菜单管理' }
          },
          {
            path: 'edit/:id(\\d+)',
            name: 'menuEdit',
            component: () => import('@/views/system/menu/edit'),
            meta: { title: '详情' },
            hidden: true
          }
        ]
      }
    ]
  },
  {
    path: '/blog',
    component: Layout,
    name: 'blog',
    meta: {
      title: '博客管理',
      icon: 'list'
    },
    children: [

      {
        path: 'category',
        component: () => import('@/views/blog/category/index'),
        meta: { title: '博客分类' },
        children: [
          {
            path: 'list',
            name: 'categoryList',
            component: () => import('@/views/blog/category/list'),
            meta: { icon: 'component', title: '博客分类' }
          },
          {
            path: 'edit/:id(\\d+)',
            name: 'categoryEdit',
            component: () => import('@/views/blog/category/edit'),
            meta: { title: '详情' },
            hidden: true
          }
        ]
      },
      {
        path: 'article',
        component: () => import('@/views/blog/article/index'),
        meta: { title: '博客文章' },
        children: [
          {
            path: 'list',
            name: 'articleList',
            component: () => import('@/views/blog/article/list'),
            meta: { icon: 'documentation', title: '博客文章' }
          },
          {
            path: 'edit/:id(\\d+)',
            name: 'articleEdit',
            component: () => import('@/views/blog/article/edit'),
            meta: { title: '详情' },
            hidden: true
          }
        ]
      }

    ]
  },
  {
    path: '/finance',
    component: Layout,
    name: 'finance',
    meta: {
      title: '财政管理',
      icon: 'list'
    },
    children: [
      {
        path: 'millionaire',
        name: 'millionaire',
        component: () => import('@/views/finance/millionaire'),
        meta: { title: '百万年薪', icon: 'example' }

      }
    ]
  },
  {
    path: '/test',
    component: Layout,
    name: 'test',
    meta: { title: '测试页', icon: 'example' },
    children: [
      {
        path: 'test1',
        name: 'test1',
        component: () => import('@/views/demo/demo'),
        meta: { title: '测试页', icon: 'example' }

      }]

  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
